<?php
/*
 * class gdADP
*/
require_once ('image.adp.php');

class gdADP extends imageADP {
	var $img = NULL;
	
	function adp_init() {
		parent::adp_init();
	}
	//    function imagejpeg(){
	
	//	return call_user_func_array('imagejpeg',func_get_args());
	
	//    }
	
	//
	
	//    function imagepng(){
	
	//	return call_user_func_array('imagepng',func_get_args());
	
	//    }
	
	//
	
	/**
	 *  图像转换方法
	 *  @param $output_path 如果是true 返回路径，如果是false 输出转换后的内容
	 *  @param $fileName 如果false：使用原来的名字，如果字符串则新的名字
	 *  @param $threshold 转换的门槛 0-8 默认8
	 */
	
	function imageConversion($imagePath, $destMime, $output_path = FALSE, $destWidth = NULL, $destHeight = NULL, $threshold = 8, $fileName = NULL) {
		///支持互相转换的mime类型
		$supportConverEathother = array(
			'image/gif',
			'image/jpeg',
			'image/png',
			'image/bmp',
			'image/x-icon',
			'image/vnd.microsoft.icon'
		);
		///其它支持的方向
		$supportConverDire = array(
			'image/jpeg->image/vnd.wap.wbmp',
			'image/png->image/vnd.wap.wbmp'
		);
		list($sourceMime, $width, $height) = $this->getimageInfo($imagePath, array(
			'mime',
			'width',
			'height'
		));
		//var_dump($sourceMime);
		if (!in_array($sourceMime, $supportConverEathother) && !in_array(sprintf('%s->%s', $sourceMime, $destMime) , $supportConverDire)) {
			//return FALSE;
			throw new Exception(sprintf('%s->%s image conversion is not supported', $sourceMime, $destMime));
			return FALSE;
		}
		if ($destHeight == NULL) $destHeight = $height;
		if ($destWidth == NULL) $destWidth = $width;
		if ($fileName == NULL) $fileName = filename($imagePath, FALSE);
		$fileExt = $this->image_type_to_extension($this->image_mime_type_to_type($destMime));
		//不同目录相同文件名怎么办
		$targetPath = __DIR_VAR_IMG_GEN___ . $fileName . $fileExt;
		if ($sourceMime == 'image/jpeg' && $destMime == 'image/vnd.wap.wbmp') {
			if ($output_path == TRUE) {
				$rbt = jpeg2wbmp($imagePath, $targetPath, $destHeight, $destWidth, $threshold);
				if ($rbt) echo getWebURL($targetPath, TRUE);
				else echo NULL;
			}
			else {
				$gdEcho = TRUE;
				if ($rbt) echo getWebURL($targetPath, TRUE);
				else echo NULL;
			}
		}
		elseif ($sourceMime == 'image/png' && $destMime == 'image/vnd.wap.wbmp') {
			if ($output_path == TRUE) {
				$rbt = png2wbmp($imagePath, $targetPath, $destHeight, $destWidth, $threshold);
			}
			else {
				$gdEcho = TRUE;
			}
		}
		elseif ($sourceMime == 'image/png' && $destMime == 'image/gif') {
			if ($output_path == TRUE) {
				$this->png2gif($imagePath, $targetPath);
				return $targetPath;
			}
			else {
				return $this->png2gif($imagePath);
			}
		}
		elseif (($sourceMime == 'image/png' || $sourceMime == 'image/jpeg' || $sourceMime == 'image/gif') && $destMime == 'image/gif') {
			if ($output_path == TRUE) {
				$this->img2icon($imagePath, $targetPath);
				return $targetPath;
			}
			else {
				return $this->img2icon($imagePath);
			}
		}
		if (isset($gdEcho) && $gdEcho == TRUE) {
			/// echo the bin string
			$imgObj = imagecreatefrompng($imagePath);
			header('Content-Type: ' . image_type_to_mime_type(IMAGETYPE_WBMP));
			image2wbmp($imgObj);
			imagedestroy($imgObj);
			return;
		}
	}
	///with some noise/random dither
	
	///gif不支持alpha channel 但支持局部完全透明
	
	function png2gifPix127($png, $output_path = FALSE, $background = array(
		0xfe,
		0x3,
		0xf4
	)) {
		$size = getimagesize($png);
		$image = imagecreatefrompng($png);
		$transparentColor = imagecolorallocate($image, $background[0], $background[1], $background[2]);
		for ($x = 0;$x < $size[0];$x++) {
			for ($y = 0;$y < $size[1];$y++) {
				$alpha = (imagecolorat($image, $x, $y) & 0x7F000000) >> 24; //127 is completely TRANSPARENT, 0 opaque less than 127 are put into 0
				
				//var_dump(imagecolorsforindex($image,imagecolorat($image,$x,$y)));
				
				//DITHER!
				if ($alpha > 3 && ($alpha >= 127 - 3 || (rand(0, 127)) >= (127 - $alpha))) {
					imagesetpixel($image, $x, $y, $transparentColor);
				}
			}
		}
		imagecolortransparent($image, $transparentColor); //只能全部透明，没有一定的透明度
		if ($output_path) {
			imagegif($image, $output_path);
		}
		else {
			imagegif($image);
		}
		imagedestroy($image);
	}
	
	function png2gif($png, $output_path = FALSE, $background = array(
		255,
		255,
		255
	)) { //白色背景？？不可以透明吗
		$size = getimagesize($png);
		$img = imagecreatefrompng($png);
		$image = imagecreatetruecolor($width = $size[0], $height = $size[1]);
		imagefill($image, 0, 0, $bgcolor = imagecolorallocate($image, $background[0], $background[1], $background[2]));
		imagecopyresampled($image, $img, 0, 0, 0, 0, $width, $height, $width, $height);
		imagecolortransparent($image, $bgcolor); //put all same color
		if ($output_path) {
			imagegif($image, $output_path);
		}
		else {
			imagegif($image);
		}
		imagedestroy($image);
	}
	/**
	 *  图像对象转 icon
	 */
	
	function img2icon($img_path, $output_path = FALSE, $desired_bit_count = 8) {
		$_image = @imagecreatefrompng($img_path) || @imagecreatefromgif($img_path) || @imagecreatefromjpeg($img_path);
		require_once (__PLU__ . 'floicon' . __SEP__ . 'floIcon.php');
		$icon_file = new floIcon();
		$icon_file->addImage($_image, $desired_bit_count);
		$icon_data = $icon_file->formatICO();
		if ($output_path) {
			file_put_contents($output_path, $icon_data);
		}
		else {
			echo $icon_data;
		}
		imagedestroy($image);
	}
	
	function getInfo() {
		//$arr=parent::getInfo();
		$arr['gd_info'] = gd_info();
		return $arr;
	}
	/**
	 *  $return 是明确需要返回的单一或者某些属性列表
	 */
	
	function getimageInfo($imgPath, $return = NULL) {
		$ret = getimagesize($imgPath);
		$arr = array(
			'width' => $ret[0],
			'height' => $ret[1],
			'type' => $ret[2],
			'bits' => $ret['bits'],
			'mime' => $ret['mime'],
			'channels' => CK($ret, 'channels')
		);
		if (is_string($return)) {
			return CK($arr, $return, "{$return} is not support");
		}
		elseif (is_array($return)) {
			return array_gets($return, $arr, FALSE);
			//return array_filter($ret,);
			
		}
		else {
			return $arr;
		}
		//var_dump($ret);
		
	}
	
	function image_type_to_mime_type($type) {
		return image_type_to_mime_type($type);
	}
	
	function getThumbnail($imagePath, &$width = NULL, &$height = NULL, &$type = NULL) {
		//now support TIFF and JPEG
		$ret = FALSE;
		if (is_callable('exif_thumbnail')) {
			$mime = $this->getimageInfo($imagePath, 'mime');
			if ($mime == 'image/tiff' || $mime == 'image/jpeg') {
				$ret = call_user_func_array('exif_thumbnail', func_get_args());
			}
		}
		if ($ret == FALSE) {
			///手动获取缩略图
			
		}
	}
	
	function image_type_to_extension() {
		return call_user_func_array('image_type_to_extension', func_get_args());
	}
}
